namespace OasisCiq.XS
{
    using System;
    using System.Xml;

    public class XSBoolean
    {
        public XSBoolean()
            : this(false)
        {
        }

        public XSBoolean(bool value)
        {
            Value = value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException">With inner <see cref="FormatException"/></exception>
        public XSBoolean(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException();
            }

            try
            {
                Value = XmlConvert.ToBoolean(value);
            }
            catch (FormatException ex)
            {
                throw new ArgumentException(ex.Message, ex);
            }
        }

        public bool Value { get; set; }

        public static implicit operator bool(XSBoolean argument)
        {
            if (argument == null)
            {
                throw new ArgumentNullException();
            }

            return argument.Value;
        }

        public static implicit operator XSBoolean(bool argument)
        {
            return new XSBoolean(argument);
        }

        public override string ToString()
        {
            return XmlConvert.ToString(Value);
        }
    }
}